package com.example.gybicycle.common.config;


import com.example.gybicycle.common.security.AuthFilter;
import com.example.gybicycle.common.security.AuthRealm;
import com.example.gybicycle.common.util.LoginUtil;
import com.google.common.collect.Lists;
import jakarta.servlet.Filter;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.pam.FirstSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @author jqllxew
 * 2020/10/22.
 */
@Configuration
public class ShiroConfig {
    //自定义matcher
    private CredentialsMatcher credentialsMatcher(){
        return  (authenticationToken, authenticationInfo) -> {
            String token = (String) authenticationToken.getCredentials();
            return LoginUtil.isLogin(token);
        };
    }
    //将自己的验证方式加入容器
    @Bean
    public AuthRealm authRealm() {
        return new AuthRealm(credentialsMatcher());
    }
    @Bean
    public ModularRealmAuthenticator authenticator(){
        ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
        //设置多个realm认证策略，一个成功即跳过其它的
        authenticator.setAuthenticationStrategy(new FirstSuccessfulStrategy());
        return authenticator;
    }
    //权限管理，配置主要是Realm的管理认证
    @Bean
    public DefaultSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setAuthenticator(authenticator());
        securityManager.setRealms(Lists.newArrayList(authRealm()));
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        securityManager.setSubjectDAO(subjectDAO);
        return securityManager;
    }
    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String,String> map = new HashMap<>();
//        map.put("/**", "anon");
        map.put("/doc.html", "anon");
        map.put("/webjars/**/**", "anon");
        map.put("/webjars/**", "anon");
        map.put("/static/**", "anon");
        map.put("/select/**", "anon");
        map.put("/wx/**", "anon");
        map.put("/web/**","noSessionCreation,fil");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        // 添加自己的过滤器
        Map<String, Filter> filterMap = new HashMap<>(1);
        filterMap.put("fil", new AuthFilter());
        shiroFilterFactoryBean.setFilters(filterMap);
        //登录
        shiroFilterFactoryBean.setLoginUrl("/login");
        return shiroFilterFactoryBean;
    }

    //加入注解的使用
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
